/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.action.changemapping;

import com.inspur.edp.bff.api.dataprocessor.IEntityPropertyMapper;
import com.inspur.edp.bff.api.manager.context.MessageMapperContext;
import com.inspur.edp.cef.api.message.BizMessage;
import com.inspur.edp.cef.api.message.IBizMessage;
import com.inspur.edp.cef.api.message.MessageLocation;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DefaultMessageMapAction extends MessageMappingAction {

    public DefaultMessageMapAction(MessageMapperContext context) {
        super(context);
    }

    @Override
    protected void execute() {
        if (getMapContext().getSourceMessage() == null) {
            return;
        }
        IEntityPropertyMapper rootMapper = getContext().getRootEntityPropertyMapper();
        if (rootMapper == null) {
            return;
        }

        List<IBizMessage> defaultMessages = null;
        for (Map.Entry<String, List<IBizMessage>> pair : getMapContext().getSourceMessage()
                .entrySet()) {
            if (pair.getValue() == null || pair.getValue().isEmpty()) {
                continue;
            }

            final String beType =
                    "Default".equals(pair.getKey()) ? rootMapper.getBEInfo().getBEType() : pair.getKey();

            for (IBizMessage msg : pair.getValue()) {
                if (msg.getLocation() == null) {
                    getMapContext().getTargetMessage().add(msg);
                    continue;
                }

                IEntityPropertyMapper mapper = rootMapper
                        .getMapperByBECode(beType, msg.getLocation().getNodeCode());
                if (mapper == null) {
                    continue;
                }
                BizMessage targetMsg = new BizMessage() {{
                    setLevel(msg.getLevel());
                    setMessageFormat(msg.getMessageFormat());
                    setMessageParams(msg.getMessageParams());
                    setException(msg.getException());
                    setLocation(new MessageLocation());
                }};
                targetMsg.getLocation().setNodeCode(mapper.getVOCode());
                if (msg.getLocation().getDataIds() != null) {
                    targetMsg.getLocation().setDataIds(new ArrayList<>(msg.getLocation().getDataIds()));
                }
                if (msg.getLocation().getColumnNames() != null) {
                    targetMsg.getLocation().setColumnNames(msg.getLocation().getColumnNames().stream()
                            .map(col -> mapper.getVOPropertyNameFromBE(beType, col)).collect(
                                    Collectors.toList()));
                }
                //TODO: 运行时定制暂不支持
                //dealExtendMapping(voChange, defaultChange);
                getMapContext().getTargetMessage().add(targetMsg);
            }
        }
    }
}
